<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Example 03</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>Example 03</h1>
    <hr>
    <div id="app">
        {{num}}
        <p><button @click="add">Add</button></p>
    </div>
    <p><button onclick="reduce()">Reduce</button></p>
    <p><button onclick="reduceOnce()">reduceOnce</button></p>
    <p><button onclick="off()">off</button></p>
    <script type="text/javascript">
        var app = new Vue({
            el:'#app',
            data:{
                num:1
            },
            methods:{
                add:function(){
                    this.num++
                }
            }
        });
        app.$on("reduce",function(){
            console.log("执行了reduce方法");
            this.num--;
        })

        //外部调用内部事件
        function reduce(){
            app.$emit('reduce');
        }
        app.$once('reduceOnce',function(){
            console.log('只执行一次的方法');
            this.num--;
        
        });
        function reduceOnce(){
            app.$emit('reduceOnce');
        }
        //关闭事件
        function off(){
            app.$off('reduce');
        }
    </script>
</body>
</html>